/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package linkeddata.arida.ufc.br.VCD;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import linkeddata.arida.ufc.br.manager.impl.BuildsMapJson;
import linkeddata.arida.ufc.br.manager.impl.ExecuteQueryJena;
import linkeddata.arida.ufc.br.search.FileHierarchyVCDS;
import org.apache.commons.lang3.text.WordUtils;

/**
 *
 * @author jeovane
 */
public class ExecuteVCD {

    public static void main(String[] args) throws Exception, Throwable {
//        ExecuteQueryJena queryJena;
        ElementsVCDS elementsVCDS = new ElementsVCDS();
        FileHierarchyVCDS fhv = new FileHierarchyVCDS(elementsVCDS);
        String textQuery = "";
        List<ExtractsContentVCD> extractList = new ArrayList<ExtractsContentVCD>();

        ArrayList<File> listFiles = new ArrayList<File>();
        
        listFiles.add(new File("/home/jeovane/Dropbox/NetBeansProjects/JBOS/JBOS/web/XML/VCD_2.xml"));
        listFiles.add(new File("/home/jeovane/Dropbox/NetBeansProjects/JBOS/JBOS/web/XML/VCD_3.xml"));
        listFiles.add(new File("/home/jeovane/Dropbox/NetBeansProjects/JBOS/JBOS/web/XML/VCD_1.xml"));

//        List<String> listVCDSDestination = new ArrayList<String>();

        // Adiciona objetos ExtractsContentVCD a uma List
        for (int i = 0; i < listFiles.size(); i++) {
            ExtractsContentVCD e = new ExtractsContentVCD(listFiles.get(i));
            elementsVCDS.setExtractList(e);
        }

//        for (String s : fhv.CorrectHierarchyVCDS()) {
//            for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//                if (elementsVCDS.getExtractList().get(i).getNameVCD().equalsIgnoreCase(s)) {
//                    //System.out.println(elementsVCDS.getExtractList().get(i).getNameVCD());
//                    extractList.add(elementsVCDS.getExtractList().get(i));
//                }
//            }
//        }
//        
        String nameVCD = "diseases-name";
        String service = "http://localhost:3030/diseasome/sparql";
        String select = "";
        String[] filters = {"drugs-name ?:http://localhost:3030/diseasome/sparql ?:FILTER regex(?Disease_name, \"protein\", \"i\")", "drugs-name ?:http://localhost:3030/diseasome/sparql ?:FILTER regex(?Disease_name, \"vitamin\", \"i\")", "drugs-name ?:http://localhost:3030/dailymed/sparql ?:FILTER regex(?Drug_name, \"protein\", \"i\")"};

        Map<String, Set<String>> mapSet = new HashMap<String, Set<String>>();
        Set<String> set = new HashSet<String>();
        set.add("FILTER regex(?Disease_name, \"protein\", \"i\")");
        set.add("FILTER regex(?Disease_name, \"vitamin\", \"i\")");
        
        Set<String> set1 = new HashSet<String>();
        set1.add("FILTER regex(?Drug_name, \"protein\", \"i\")");
        
        mapSet.put("drugs-name ?:http://localhost:3030/diseasome/sparql", set);
        mapSet.put("drugs-name ?:http://localhost:3030/dailymed/sparql", set1);
        
        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            for (Map.Entry entry : elementsVCDS.getExtractList().get(i).getAttributesVCD().entrySet()) {
//                //System.out.println(entry.getKey().equals(nameVCD));
//                if (entry.getKey().equals(nameVCD)) {
//                    ArrayList<String> values = (ArrayList<String>) entry.getValue();
//                    select += "SELECT ";
//                    for (String v : values) {
//                        select += "?" + v + " ";
//                    }
//                }
//            }
            //System.out.println(elementsVCDS.getExtractList().get(i).getCondition());
//            for (int j = 0; j < elementsVCDS.getExtractList().get(i).getVariablesSelect().size(); j++) {
//                //System.out.print(elementsVCDS.getExtractList().get(i).getVariablesSelect().get(j) + " ");
//            }
//            System.out.println();
            
//            for (int j = 0; j < elementsVCDS.getExtractList().get(i).getElosVCD().size(); j++) {
//                System.out.println(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getParameter());
//            }
            
//            for (Map.Entry entry : elementsVCDS.getExtractList().get(i).getServicesVCDS().entrySet()) {
//                ArrayList<String> values = (ArrayList<String>) entry.getValue();
//                System.out.println(entry.getKey());
//                for (String v : values) {
//                    System.out.println("    " + v);
//                }
//                
//            }
            
//            for (int j = 0; j < elementsVCDS.getExtractList().get(i).extractQueryVCD().size(); j++) {
//                List<ElementsVCDS> extractQuery = elementsVCDS.getExtractList().get(i).extractQueryVCD();
//                
//                for (Iterator it = BuildsMapJson.JSONMap(jsonTextQuery).entrySet().iterator(); it.hasNext();) {
//                    String filter = extractQuery.get(j).getNameVCDExtract() + " ?:" + extractQuery.get(j).getService();
//                    Map.Entry entry = (Map.Entry) it.next();
//                    LinkedList<String> linked = (LinkedList<String>) entry.getValue();
//                    String fil = "";
//                    if (filter.equals(entry.getKey())) {
//                        for (String s : linked) {
//                            fil += s + "\n";
//                        }
//                        //System.out.println(extractQuery.get(j).getWhere() + fil);
//                    }
//                }
//            }
        }
        
        
        //System.out.println(elementsVCDS.getExtractList().get(0).getAttributesVCD().get(nameVCD));
        
        //System.out.println(select);

//        for (int i = 0; i < elementsVCDS.getExtractList().get(1).extractQueryVCD().size(); i++) {
//           String[] setQuery = elementsVCDS.getExtractList().get(2).extractQueryVCD().get(i).split(",", 2);
//            System.out.println(setQuery[1]);
//        }
        
        //String newQuery = elementsVCDS.getExtractList().get(2).getQueryVCD();
        //System.out.println(newQuery);
        String s2 = "PREFIX sider: <http://www4.wiwiss.fu-berlin.de/sider/resource/sider/>\n" +
"\n" +
"                                select * where {\n" +
"                                        ?sa sider:sideEffect ?se .\n" +
"                                        ?se sider:sideEffectName ?sd_eff .\n" +
"                                }";
        
//        String strInit = newQuery.substring(0, newQuery.indexOf("SELECT"));
        //String strEnd = s1.substring(s1.indexOf("?"), s1.length() - 1);
        //System.out.println(strEnd);
        
        String s1 = "PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>\n" +
"\n" +
"SELECT *\n" +
"WHERE {\n" +
"    ?gdg drugbank:chemicalFormula ?Drug_chemicalFormula.\n" +
"    OPTIONAL {\n" +
"        ?gdg drugbank:affectedOrganism ?GenericDrug_affectedOrganism\n" +
"    }\n" +
"} LIMIT 10";
        
        String testQuery = s1.substring(s1.indexOf("WHERE") + 7, s1.lastIndexOf("}"));
        //System.out.println(testQuery);

        //System.out.println(strInit + "\n" + select.toLowerCase() + "\n" + strEnd.toLowerCase());

        //System.out.println(WordUtils.capitalize("jeovane reges"));

       // StringBuffer s = new StringBuffer(newQuery);
       // s.replace(s.indexOf("SELECT"), s.indexOf("WHERE"), select);
        //System.out.println(s);

        //for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
        //System.out.println(select.toLowerCase() + "\n" + strEnd);
        //}
        
        //queryJena.setParameterList("disease_name" + "," + "Analbuminemia");
//        parameterList.add("drug_name" + "," + "Fluconazole (Injection, Solution)");
        
        //String jsonTextQuery = "{\"drugs-name ?:http://localhost:3030/diseasome/sparql\":[\"FILTER regex(?Disease_name, 'protein', 'i')\",\"FILTER regex(?Disease_name, 'vitamin', 'i')\"]}";
        
        String jsonTextQuery = "{\"diseases-name ?:http://localhost:3030/diseasome/sparql\":[\"FILTER regex(?Disease_name, 'protein', 'i')\"]}";
        
         //String jsonTextQuery = "{}";

        ExecuteQueryVCDS eQuery = new ExecuteQueryVCDS(elementsVCDS, fhv, jsonTextQuery);
        
//        for (Map.Entry entry : eQuery.queriesAlreadyExecuted(s1, s1).entrySet()) {
//            
//        }
        
        System.out.println(eQuery.getQueryVCD(0));
        
//        String so = System.getProperty("os.name");
//        
//        if (so.equals("Linux")) {
//            //System.out.println("/home/" + System.getProperty("user.name") + "/");
//            System.out.println(System.getProperty("user.home") + "/" + System.getProperty("user.name") + "_" + elementsVCDS.getExtractList().get(0).getNameVCD() + ".txt");
//        } else {
//            System.out.println("ok");
//        }

        //System.out.println(eQuery.executeQueryVCD());
        //eQuery.executeQueryVCD();
        //System.out.println(eQuery.executeQueryVCD());
        //eQuery.queryParametersElos();
        //eQuery.queryParametersSchema();

//        for (String query : eQuery.executeQueryVCD()) {
//            System.out.println(query);
//        }

//        for (String s : eQuery.pathBetweenVCDS(1)) {
//            System.out.println(s);
//        }

//        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            System.out.print(elementsVCDS.getExtractList().get(i).getNameVCD() + ": ");
//            for (String s : eQuery.getDestinationVCD(i)) {
//                System.out.print(s + " ");
//            }
//            System.out.println();
//        }

        for (int i = 0; i < eQuery.extractDestinationVCD(0).size(); i++) {
            //System.out.println(eQuery.extractDestinationVCD(0).get(i));
        }

//        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            for (String s : eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i))) {
//                System.out.println(s);
//            }
//        }

//        for (Iterator it = eQuery.queryParametersElos().entrySet().iterator(); it.hasNext();) {
//            Map.Entry entry = (Map.Entry) it.next();
//            System.out.println(entry.getKey());
//            List<String> listValue = (List<String>) entry.getValue();
//            for (String v : listValue){
//                System.out.println("  " + v);
//            }
//        }

//        eQuery.buildsFilterVCD();
        //eQuery.getDestinationVCD();

//        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            //System.out.println(eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i)));
//            //eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i));
//            for (int j = 0; j < eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i)).size(); j++) {
//                System.out.println(eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i)).get(j));
//            }
////            System.out.println(eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i)));
//        }

//        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
////            System.out.println(eQuery.getDestinationVCD(elementsVCDS.getExtractList().get(i)));
////            System.out.println(elementsVCDS.getExtractList().get(i).getNameVCD() + ": " + eQuery.sizeDestinationVCD(elementsVCDS.getExtractList().get(i)));
////            System.out.println(elementsVCDS.getExtractList().get(i));
//            for (int j = 0; j < eQuery.sizeDestinationVCD(elementsVCDS.getExtractList().get(i)); j++) {
//                if (!"".equals(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getDestinationVCD())) {
//                    System.out.println(elementsVCDS.getExtractList().get(i).getNameVCD() + " "
//                            + elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getDestinationVCD()
//                            + " " + elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getParameter());
//                }
//            }
//        }

//        for (int i = 0; i < elementsVCDS.getListNamesVCDS().size(); i++){
//            System.out.println(elementsVCDS.getListNamesVCDS().indexOf(elementsVCDS.getListNamesVCDS().get(i)));
//        }

//        int i = 0;
//        for (String s : fhv.CorrectHierarchyVCDS()){
//            System.out.println(i + ": " + s);
//            i++;
//        }

//        for (String nodes : fhv.CorrectHierarchyVCDS()) {
//            for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//                if (nodes.equalsIgnoreCase(elementsVCDS.getExtractList().get(i).getElosVCD().get(0).getNameVCDElos())) {
//                    queryJena = new ExecuteQueryJena("http://localhost:3030/dailymed/sparql", elementsVCDS.getExtractList().get(i).getQueryVCD());
//                    System.out.println(queryJena.printQuery());
//                }
//            }
//        }

        // Obtém a VCD cujo valor do atributo primary é ON
//        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            if (elementsVCDS.getExtractList().get(i).getVCDMain() != null) {
//                listVCDSDestination.add(elementsVCDS.getExtractList().get(i).getVCDMain());
//            }
//        }

//        for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            for (int j = 0; j < elementsVCDS.getExtractList().get(i).getElosVCD().size(); j++) {
//                //for (int k = 0; k < elementsVCDS.getExtractList().size(); k++) {
//                    //System.out.println(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getNameVCDElos());
//                    if (elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getNameVCDElos().equalsIgnoreCase(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getDestinationVCD())) {
//                        listVCDSDestination.add(elementsVCDS.getExtractList().get(j).getQueryVCD());
//                    }
//                //}
//                //System.out.println(elementsVCDS.getExtractList().get(i).getNameVCDElos() + " - " + elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getDestinationVCD());
////                System.out.println(elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getNameVCDElos() 
////                        + " - " + elementsVCDS.getExtractList().get(i).getElosVCD().get(j).getDestinationVCD());
//            }
//        }

//        for (int i = 0; i < listVCDSDestination.size(); i++) {
//            queryJena = new ExecuteQueryJena("http://localhost:3030/dailymed/sparql", listVCDSDestination.get(i));
//            System.out.println(queryJena.printQuery());
//        }

//            for (int i = 0; i < elementsVCDS.getExtractList().size(); i++) {
//            queryJena = new ExecuteQueryJena("http://localhost:3030/dailymed/sparql", elementsVCDS.getExtractList().get(i).getQueryVCD());
//            System.out.println(queryJena.printQuery());
        //System.out.println(elementsVCDS.getExtractList().get(i).getNameVCDElos());
//            }

        //System.out.println(e.getNameVCDElos());
        //System.out.println(e.getAttributesVCD());
        //System.out.println(e.getQueryVCD());
        //System.out.println(queryJena.printQuery());
        //System.out.println(e.getQueryVCD(files));

        // Obter os atributos da tag <schema>
//        for (Iterator it = e.getAttributesVCD().entrySet().iterator(); it.hasNext();) {
//            Map.Entry entry = (Map.Entry) it.next();
//            textQuery += entry.getKey() + ": ";
//            ArrayList<String> values = (ArrayList<String>) entry.getValue();
//            for (String v : values){
//                if (v.endsWith("?")){
//                  v = v.replace("?", "");
//                  textQuery += v + " ";
//                } else {
//                    textQuery += v + " ";
//                }
//            }
//        }
        //System.out.println(textQuery);
    }
}
